home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / sound / rsynth22.zip / NSYNTH.C < prev    next >
Text File  |  1995-12-19  |  32KB  |  1,067 lines

  1. #include <config.h>
  2.  
  3.  
  4. /* $Id: nsynth.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $
  5.  */
  6. char *nsynth_id = "$Id: nsynth.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $";
  7.  
  8. /* Copyright            1982                    by Dennis H. Klatt
  9.  
  10.  *      Klatt synthesizer
  11.  *         Modified version of synthesizer described in
  12.  *         J. Acoust. Soc. Am., Mar. 1980. -- new voicing
  13.  *         source.
  14.  *
  15.  * Edit history
  16.  * 000001 10-Mar-83 DK  Initial creation.
  17.  * 000002  5-May-83 DK  Fix bug in operation of parallel F1
  18.  * 000003  7-Jul-83 DK  Allow parallel B1 to vary, and if ALL_PARALLEL,
  19.  *                      also allow B2 and B3 to vary
  20.  * 000004 26-Jul-83 DK  Get rid of mulsh, use short for VAX
  21.  * 000005 24-Oct-83 DK  Split off parwavtab.c, change short to int
  22.  * 000006 16-Nov-83 DK  Make samrate a variable, use exp(), cos() rand()
  23.  * 000007 17-Nov-83 DK  Convert to float, remove  cpsw, add set outsl
  24.  * 000008 28-Nov-83 DK  Add simple impulsive glottal source option
  25.  * 000009  7-Dec-83 DK  Use spkrdef[7] to select impulse or natural voicing
  26.  *                       and update cascade F1,..,F6 at update times
  27.  * 000010 19-Dec-83 DK  Add subroutine no_rad_char() to get rid of rad char
  28.  * 000011 28-Jan-84 DK  Allow up to 8 formants in cascade branch F7 fixed
  29.  *                       at 6.5 kHz, F8 fixed at 7.5 kHz
  30.  * 000012 14-Feb-84 DK  Fix bug in 'os' options so os>12 works
  31.  * 000013 17-May-84 DK  Add G0 code
  32.  * 000014 12-Mar-85 DHW modify for Haskins environment
  33.  * 000015 11-Jul-87 LG  modificiations for PC
  34.  * 000016 20-Apr-91 ATS Modified for SPARCSTATION
  35.  */
  36.  
  37. #include <useconfig.h>
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include "proto.h"
  41. #include "nsynth.h"
  42. #ifndef PI
  43. #ifndef M_PI                      /* <math.h> */
  44. #define PI               3.1415927
  45. #else /* M_PI */
  46. #define PI               M_PI
  47. #endif /* M_PI */
  48. #endif
  49.  
  50. #ifdef __STDC__
  51. #define ONE 1.0F
  52. #else
  53. #define ONE 1.0
  54. #endif
  55.  
  56. typedef struct
  57.  {
  58.   char *name;
  59.   float a;
  60.   float b;
  61.   float c;
  62.   float p1;
  63.   float p2;
  64.  }
  65. resonator_t, *resonator_ptr;
  66.  
  67. /* Various global variables */
  68.  
  69. int time_count = 0;
  70. static warnsw;                    /* JPI added for f0 flutter */
  71.  
  72. /* COUNTERS */
  73.  
  74. static long nper;                 /* Current loc in voicing period   40000 samp/s */
  75.  
  76. /* COUNTER LIMITS */
  77.  
  78. static long T0;                   /* Fundamental period in output samples times 4 */
  79. static long nopen;                /* Number of samples in open phase of period  */
  80. static long nmod;                 /* Position in period to begin noise amp. modul */
  81.  
  82. /* Variables that have to stick around for awhile, and thus locals
  83.    are not appropriate 
  84.  */
  85.  
  86. /* Incoming parameter Variables which need to be updated synchronously  */
  87.  
  88. static long F0hz10;               /* Voicing fund freq in Hz  */
  89. static long F0hz10org;            /* Original Fundamental Freq */
  90. static long AVdb;                 /* Amp of voicing in dB,    0 to   70  */
  91. static long Kskew;                /* Skewness of alternate periods,0 to   40  */
  92.  
  93. /* Various amplitude variables used in main loop */
  94.  
  95. static float amp_voice;           /* AVdb converted to linear gain  */
  96. static float amp_bypas;           /* AB converted to linear gain  */
  97. static float par_amp_voice;       /* AVpdb converted to linear gain  */
  98. static float amp_aspir;           /* AP converted to linear gain  */
  99. static float amp_frica;           /* AF converted to linear gain  */
  100. static float amp_breth;           /* ATURB converted to linear gain  */
  101.  
  102. /* State variables of sound sources */
  103.  
  104. static long skew;                 /* Alternating jitter, in half-period units  */
  105.  
  106. static float natglot_a;           /* Makes waveshape of glottal pulse when open  */
  107. static float natglot_b;           /* Makes waveshape of glottal pulse when open  */
  108. static float vwave;               /* Ditto, but before multiplication by AVdb  */
  109. static float vlast;               /* Previous output of voice  */
  110. static float nlast;               /* Previous output of random number generator  */
  111. static float glotlast;            /* Previous value of glotout  */
  112. static float decay;               /* TLTdb converted to exponential time const  */
  113. static float onemd;               /* in voicing one-pole low-pass filter  */
  114. static float minus_pi_t;          /* func. of sample rate */
  115. static float two_pi_t;            /* func. of sample rate */
  116.  
  117.  
  118. /* INTERNAL MEMORY FOR DIGITAL RESONATORS AND ANTIRESONATOR  */
  119.  
  120. static resonator_t rnpp =
  121. {"parallel nasal pole"};
  122. static resonator_t r1p =
  123. {"parallel 1st formant"};
  124. static resonator_t r2p =
  125. {"parallel 2nd formant"};
  126. static resonator_t r3p =
  127. {"parallel 3rd formant"};
  128. static resonator_t r4p =
  129. {"parallel 4th formant"};
  130. static resonator_t r5p =
  131. {"parallel 5th formant"};
  132. static resonator_t r6p =
  133. {"parallel 6th formant"};
  134. static resonator_t r1c =
  135. {"cascade 1st formant"};
  136. static resonator_t r2c =
  137. {"cascade 2nd formant"};
  138. static resonator_t r3c =
  139. {"cascade 3rd formant"};
  140. static resonator_t r4c =
  141. {"cascade 4th formant"};
  142. static resonator_t r5c =
  143. {"cascade 5th formant"};
  144. static resonator_t r6c =
  145. {"cascade 6th formant"};
  146. static resonator_t r7c =
  147. {"cascade 7th formant"};
  148. static resonator_t r8c =
  149. {"cascade 8th formant"};
  150. static resonator_t rnpc =
  151. {"cascade nasal pole"};
  152. static resonator_t rnz =
  153. {"cascade nasal zero"};
  154. static resonator_t rgl =
  155. {"crit-damped glot low-pass filter"};
  156. static resonator_t rlp =
  157. {"downsamp low-pass filter"};
  158. static resonator_t rout =
  159. {"output low-pass"};
  160.  
  161. /*
  162.  * Constant natglot[] controls shape of glottal pulse as a function
  163.  * of desired duration of open phase N0
  164.  * (Note that N0 is specified in terms of 40,000 samples/sec of speech)
  165.  *
  166.  *    Assume voicing waveform V(t) has form: k1 t**2 - k2 t**3
  167.  *
  168.  *    If the radiation characterivative, a temporal derivative
  169.  *      is folded in, and we go from continuous time to discrete
  170.  *      integers n:  dV/dt = vwave[n]
  171.  *                         = sum over i=1,2,...,n of { a - (i * b) }
  172.  *                         = a n  -  b/2 n**2
  173.  *
  174.  *      where the  constants a and b control the detailed shape
  175.  *      and amplitude of the voicing waveform over the open
  176.  *      potion of the voicing cycle "nopen".
  177.  *
  178.  *    Let integral of dV/dt have no net dc flow --> a = (b * nopen) / 3
  179.  *
  180.  *    Let maximum of dUg(n)/dn be constant --> b = gain / (nopen * nopen)
  181.  *      meaning as nopen gets bigger, V has bigger peak proportional to n
  182.  *
  183.  *    Thus, to generate the table below for 40 <= nopen <= 263:
  184.  *
  185.  *      natglot[nopen - 40] = 1920000 / (nopen * nopen)
  186.  */
  187. static const short natglot[224] =
  188. {
  189.  1200, 1142, 1088, 1038, 991, 948, 907, 869, 833, 799,
  190.  768, 738, 710, 683, 658, 634, 612, 590, 570, 551,
  191.  533, 515, 499, 483, 468, 454, 440, 427, 415, 403,
  192.  391, 380, 370, 360, 350, 341, 332, 323, 315, 307,
  193.  300, 292, 285, 278, 272, 265, 259, 253, 247, 242,
  194.  237, 231, 226, 221, 217, 212, 208, 204, 199, 195,
  195.  192, 188, 184, 180, 177, 174, 170, 167, 164, 161,
  196.  158, 155, 153, 150, 147, 145, 142, 140, 137, 135,
  197.  133, 131, 128, 126, 124, 122, 120, 119, 117, 115,
  198.  113, 111, 110, 108, 106, 105, 103, 102, 100, 99,
  199.  97, 96, 95, 93, 92, 91, 90, 88, 87, 86,
  200.  85, 84, 83, 82, 80, 79, 78, 77, 76, 75,
  201.  75, 74, 73, 72, 71, 70, 69, 68, 68, 67,
  202.  66, 65, 64, 64, 63, 62, 61, 61, 60, 59,
  203.  59, 58, 57, 57, 56, 56, 55, 55, 54, 54,
  204.  53, 53, 52, 52, 51, 51, 50, 50, 49, 49,
  205.  48, 48, 47, 47, 46, 46, 45, 45, 44, 44,
  206.  43, 43, 42, 42, 41, 41, 41, 41, 40, 40,
  207.  39, 39, 38, 38, 38, 38, 37, 37, 36, 36,
  208.  36, 36, 35, 35, 35, 35, 34, 34, 33, 33,
  209.  33, 33, 32, 32, 32, 32, 31, 31, 31, 31,
  210.  30, 30, 30, 30, 29, 29, 29, 29, 28, 28,
  211.  28, 28, 27, 27
  212. };
  213.  
  214. /*
  215.  * Convertion table, db to linear, 87 dB --> 32767
  216.  *                                 86 dB --> 29491 (1 dB down = 0.5**1/6)
  217.  *                                 ...
  218.  *                                 81 dB --> 16384 (6 dB down = 0.5)
  219.  *                                 ...
  220.  *                                  0 dB -->     0
  221.  *
  222.  * The just noticeable difference for a change in intensity of a vowel
  223.  *   is